#!/bin/bash

SCRIPT_PATH=$(dirname $(readlink -f $0))
WORK_PATH="/opt/middleware/AtlasEdge"
#site程序
ATLASEDGE_SITE_BIN=${WORK_PATH}/software/edge_site/postern_main.py

. ${SCRIPT_PATH}/log_util.sh

function exit_trap()
{
    trap - INT TERM EXIT QUIT ABRT
    logger_Info "ha_status_monitor.sh exit pid: $$"
    kill -9 $$
}

function get_ha_home_path()
{
    cat /etc/profile | grep MINDX_EDGE_HA_HOME | awk -F '=' '{print $2}'
}

main()
{
    logger_Info "start ha status monitor"
    trap exit_trap INT TERM EXIT QUIT ABRT

    ha_old_home_path=$(get_ha_home_path)
    ps -ef | grep "inotifywait" | grep "/etc/profile" | grep -v grep | awk '{print $2}' | xargs -n1 kill -9

    while true; do
        inotifywait -q -e modify,attrib,move,close_write,create,delete,delete_self /etc/profile > /dev/null 2>&1
        sync
        ha_home_path=$(get_ha_home_path)
        if [ "$ha_old_home_path" != "$ha_home_path" ]; then
            ha_old_home_path=$ha_home_path
            logger_Info "ha home path changed, send ha reload signal to postern main $postern_pid"
            sudo pkill -10 -f ${ATLASEDGE_SITE_BIN}
        fi
    done
}

main "$@"
RESULT=$?
